package com.example.tuike.controller;
import com.example.tuike.entity.Contents;
import com.example.tuike.service.ContentsService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/2/2 21:27

 * @Version 1.0*/




@RestController
@RequestMapping("/api/contents")
public class ContentsController {

    @Autowired
    private ContentsService contentsService;

    @GetMapping
    public ResponseEntity<List<Contents>> getAllContents() {
        List<Contents> contents = contentsService.getAllContents();
        return ResponseEntity.ok(contents);
    }
    @Operation(summary = "通过订阅源ID获取内容列表，每次返回最新的20条数据")
    @GetMapping("/feed/{feedId}")
    public ResponseEntity<List<Contents>> getContentsByUserIdAndFeedId(
            @PathVariable Integer feedId,
            @RequestParam(value = "cursor", required = false) Long cursor,
            @RequestParam(value = "limit", defaultValue = "20") Integer limit) {
        List<Contents> contents = contentsService.getContentsByUserIdAndFeedId(feedId, cursor, limit);
        if (contents.isEmpty()) {
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.ok(contents);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Contents> getContentById(@PathVariable Long id) {
        Contents content = contentsService.getContentsById(id);
        if (content == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(content);
    }

    @PostMapping
    public ResponseEntity<Contents> createContent(@RequestBody Contents content) {
        Contents createdContent = contentsService.createContent(content);
        return ResponseEntity.ok(createdContent);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Contents> updateContent(@PathVariable Long id, @RequestBody Contents content) {
        content.setContentId(id);
        Contents updatedContent = contentsService.updateContent(content);
        return ResponseEntity.ok(updatedContent);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteContent(@PathVariable Long id) {
        contentsService.deleteContent(id);
        return ResponseEntity.noContent().build();
    }
}